//初始化全局参数

var url="http://47.105.48.86:8081/ad-proxy";//接口url
var param = "";//接口方法名
var datas = {};//ajax入参
var method = "";//请求方式post/get
var dataResult = {};//ajax响应后台返回值
var dataResult2 = {};//备用2
var dataResult3 = {};//备用3
var sucmsg = "";
//	获取地址栏参数
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  decodeURI(r[2]); return null;
}
//	获取token
var token=GetQueryString("token");
if(token == "" || token == null || token == undefined){
//	var local = window.location.href;
//	console.log(local);
	var sss = window.location.host;
	var sss1 = window.location.pathname;
	sss = sss+sss1;
//	console.log(sss);
	
	//window.location.href= "http://"+sss+"?token=ff17831f-7a97-4dbc-81a5-469f021ac84a&id=5";
	layer.msg('用户信息已失效',{time:1000},function() {
		window.location.href = "../../public/login.html";
	});
	
//	location.href=src+;
}
var token_id=GetQueryString("id");
var token_str="?token="+token+"&id="+token_id;
//通用页面跳转
$(".href_url").on("click",function(){
	location.href=$(this).attr("data")+token_str;
});
//title弹框
function tips(event,str) {
    layer.tips(str, $(event.target), {
        time: 2000,
        tips: [1, '#fffee3']
    });
}
//成功刷新页面
function isSuc(res){
	layer.msg(res.msg);
	setTimeout(function(){ 
		window.location.reload(); 
	}, 2000);
}
//	错误提示
function isErr(res) {
	layer.msg(res.msg);
	console.log("code非0错误=="+res.msg);
	
//	if(res.code == "0020"){
//		location.href="login.html";
//	}
}

//封装的ajax方法---全局调用---
function getAgentMsg(param,method,datas){
	$.ajax({
		url: url + param,
		type: method,
		//		参数需转为字符串
		//		json跨域
		dataType: 'json',
		data:JSON.stringify(datas),
		async:false,
		//		contentType类型为json
		contentType: "application/json",
		useDefaultXhrHeader: false,
		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
		

			if(result.code == '0') {
				dataResult = result;
//				console.log(dataResult);
				
//				layer.msg('退出成功',{time:2*1000},function() {
//					location.href='login.html';
//				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}


//备用的ajax2.
function getAgentMsg2(param2,method2,datas2){
	$.ajax({
		url: url + param2,
		type: method2,
		dataType: 'json',
		data:JSON.stringify(datas2),
		async:false,
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
//		console.log(result);
			if(result.code == '0') {
				dataResult2 = result;
//				layer.msg('退出成功',{time:2*1000},function() {
//					location.href='login.html';
//				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}

//备用的ajax3.
function getAgentMsg3(param3,method3,datas3){
	$.ajax({
		url: url + param3,
		type: method3,
		dataType: 'json',
		data:JSON.stringify(datas3),
		async:false,
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
//			console.log(result);
			if(result.code == '0') {
				dataResult3 = result;

			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}

//获取当前角色的用户余额

function getBalance(userid){
	$.ajax({
		url: url+'/sys/account',
		type: "post",
		//		参数需转为字符串
		//		json跨域
		dataType: 'json',
		data:JSON.stringify(datas),

		//		contentType类型为json
		contentType: "application/json",
		useDefaultXhrHeader: false,
		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
		
			if(result.code == '0') {
				$('.user-list').children().first().html("余额："+result.data.amount);
				
//				layer.msg('退出成功',{time:2*1000},function() {
//					location.href='login.html';
//				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}
getBalance(token_id);
getAgUserInfo();


//获取当前代理商账号基本信息
function getAgUserInfo(){
	$.ajax({
		url: url+'/agency/findAgUserInfo',
		type: "post",
		//		参数需转为字符串
		//		json跨域
		dataType: 'json',
		data:JSON.stringify(datas),

		//		contentType类型为json
		contentType: "application/json",
		useDefaultXhrHeader: false,
		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
		
			if(result.code == '0') {
		
				$('.user-info').children().first().text(result.data.agPerson);
				$('.user-info').children().first().next().text(result.data.agCompayName);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}

//退出方法通用
function logout(){
	$.ajax({
		url: url+'/sys/logout',
		type: "get",
		dataType: 'json',
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
			if(result.code == '0') {
				layer.msg('退出成功',{time:2*1000},function() {
					window.location.href = '../../public/login.html';
				});	
			}
		}
	});
}
$(".user-list").children().next("li").on("click",function(){
	logout();
})




//下载excel文件
function exportCsv(obj){
    //title ["","",""]
    var title = obj.title;
    //titleForKey ["","",""]
    var titleForKey = obj.titleForKey;
    var data = obj.data;
    var str = [];
    str.push(obj.title.join(",")+"\n");
    for(var i=0;i<data.length;i++){
        var temp = [];
        for(var j=0;j<titleForKey.length;j++){
            temp.push(data[i][titleForKey[j]]);
    }
    str.push(temp.join(",")+"\n");
    }
    var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(str.join(""));  
    var downloadLink = document.createElement("a");
    downloadLink.href = uri;
    downloadLink.download = "export.csv"; 
    document.body.appendChild(downloadLink);
    downloadLink.click();
    document.body.removeChild(downloadLink); 
}